﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MYTech.Data
{
    /// <summary>
    /// 时间类
    /// </summary>
    public class TimePeriod
    {
        #region 时间属性
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
        /// <summary>
        /// 时间间隔
        /// </summary>
        public TimeSpan TimeSpan
        {
            get { return EndTime - StartTime; }
        }

        public TimePeriod()
        {
            this.EndTime = DateTime.Now;
            this.StartTime = this.EndTime.AddMinutes(-1);
        }

        public TimePeriod(DateTime startTime, DateTime endTime)
        {
            this.StartTime = startTime;
            this.EndTime = endTime;
        }

        /// <summary>
        /// 是否有效，开始时间小于结束时间时有效
        /// </summary>
        public bool IsValid
        {
            get
            {
                return StartTime < EndTime;
            }
        }

        /// <summary>
        /// 是否为整一小时
        /// </summary>
        public bool Is1Hour
        {
            get
            {
                return StartTime.AddHours(1) == EndTime && StartTime.AddHours(-StartTime.Hour).TimeOfDay.Ticks == 0;
            }
        }

        /// <summary>
        /// 是否为整一天
        /// </summary>
        public bool Is1Day
        {
            get
            {
                return StartTime.TimeOfDay.Ticks == 0 && StartTime.AddDays(1) == EndTime;
            }
        }

        /// <summary>
        /// 是否为整一周
        /// </summary>
        public bool Is1Week
        {
            get
            {
                return StartTime.TimeOfDay.Ticks == 0 && StartTime.DayOfWeek == DayOfWeek.Monday && StartTime.AddDays(7) == EndTime;
            }
        }

        /// <summary>
        /// 是否为整一月
        /// </summary>
        public bool Is1Month
        {
            get
            {
                return StartTime.TimeOfDay.Ticks == 0 && StartTime.Day == 1 && StartTime.AddMonths(1) == EndTime;
            }
        }

        /// <summary>
        /// 起始结束时间同步增加days天
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public TimePeriod AddDays(double days)
        {
            return new TimePeriod
            {
                StartTime = StartTime.AddDays(days),
                EndTime = EndTime.AddDays(days)
            };
        }

        /// <summary>
        /// 起始结束时间同步增加months月
        /// </summary>
        /// <param name="months"></param>
        /// <returns></returns>
        public TimePeriod AddMonths(int months)
        {
            return new TimePeriod
            {
                StartTime = StartTime.AddMonths(months),
                EndTime = EndTime.AddMonths(months)
            };
        }

        /// <summary>
        /// 时间并集
        /// </summary>
        /// <param name="timePeriod"></param>
        /// <returns></returns>
        public List<TimePeriod> Merge(TimePeriod timePeriod)
        {
            if (StartTime <= timePeriod.StartTime && EndTime >= timePeriod.EndTime)
                return new List<TimePeriod>() { this };
            bool merged = false;
            if (timePeriod.StartTime <= EndTime && EndTime < timePeriod.EndTime)
            {
                EndTime = timePeriod.EndTime;
                merged = true;
            }
            if (timePeriod.StartTime < StartTime && StartTime <= timePeriod.EndTime)
            {
                StartTime = timePeriod.StartTime;
                merged = true;
            }
            if (merged)
                return new List<TimePeriod>() { this };
            else
                return new List<TimePeriod>() { this, timePeriod };
        }

        /// <summary>
        /// 判断时间交集
        /// </summary>
        /// <param name="timePeriod"></param>
        /// <returns></returns>
        public bool Intersect(TimePeriod timePeriod)
        {
            if (StartTime >= timePeriod.StartTime && EndTime <= timePeriod.EndTime)
                return true;
            bool intersected = false;
            if (StartTime <= timePeriod.EndTime && timePeriod.EndTime < EndTime)
            {
                EndTime = timePeriod.EndTime;
                intersected = true;
            }
            if (StartTime < timePeriod.StartTime && timePeriod.StartTime <= EndTime)
            {
                StartTime = timePeriod.StartTime;
                intersected = true;
            }
            return intersected;
        }
        #endregion
    }
}
